﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Frame;
using WeifenLuo.WinFormsUI.Docking;
using System.Drawing;

namespace Frame
{

    public enum LogLevel
    {
        INFO,   //普通信息，用于记录运行过程
        API,    //运行正确的API
        WARNING,  //提示信息
        ERROR,   //错误
        FATAL,   //致命的错误
    }

    public class LogInfo
    {
        public string text;
        public LogLevel level;
        public LogInfo(string text, LogLevel level)
        {
            this.text = text; this.level = level;
        }

        public Color GetColor()
        {
            Color color = Color.Black;
            switch (level)
            {
                case LogLevel.INFO:
                    color = Color.Green;
                    break;
                case LogLevel.API:
                    color = Color.Green;
                    break;
                case LogLevel.WARNING:
                    color = Color.BlueViolet;
                    break;
                case LogLevel.ERROR:
                    color = Color.Red;
                    break;
                case LogLevel.FATAL:
                    color = Color.IndianRed;
                    break;
            }
            return color;
        }

    }

    //public delegate void LogEvent();
    public class Log
    {
        FrameApplication m_app;
        List<string> apiErrorList;//上次报错的接口

        List<LogInfo> logList = new List<LogInfo>();
        public List<LogInfo> LogList { get { return logList; } }

        public bool Enable { get; set; }
        public Log(FrameApplication app)
        {
            m_app = app;
            logList = new List<LogInfo>();
            apiErrorList = new List<string>();
            Enable = true;
        }

        public event Action LogEvent;

        //运行信息打印
        public void AddRunLog(RunInfo info, LogLevel level, string head = "", string tail = "")
        {
            string text = head +" "+ tail;
            AddLog(text, level);
        }
      
        public void AddErrInfoLog(int err_id, string head = "")
        {
            string text = head + ":" + m_app.ErrorMessage.GetErrInfo(err_id)+"　[ErrId:"+err_id.ToString()+"］";
            LogLevel level = err_id == 0 ? LogLevel.INFO : LogLevel.ERROR;
            AddLog(text, level);
        }

        private Object m = new Object();
        public void AddLog(string text, LogLevel level = LogLevel.INFO)
        {
            DateTime date = DateTime.Now;
            string str = date.ToString() + ">>" + text;
            if (Enable)
            {
                //lock (m)
                {
                    logList.Add(new LogInfo(str, level));
                    if (LogEvent != null)
                    {
                        LogEvent();
                    }
                }
            }
        }


    }
}
